#include <iostream>
#include "module.hxx"

#ifdef __cplusplus
extern "C" {
#endif
#include <unistd.h>
#include <string.h>
#ifdef __cplusplus
}
#endif

// 这是需要人工判断的模块，在run里面，会一直执行，除非被停止

int stop_loop = 0;
int testResult = TestResultAsPass;

char status[REQUEST_MAX_DATA_SIZE] = {"There are some init data. Why not run this module, and try again?"};

// 模块执行动作前的通知
void pending_notify(int action, void *arg)
{
    struct moduleActionArg *actArg = (struct moduleActionArg*) arg;
    switch (action) {
    /* 设置 模块执行结果 */
    // 如果一个模块需要手动设置执行结果，那通常也意味着此后不再需要它继续执行了。
    case ModuleActionSetRunResult:
        std::cout << "Recvived 'SetRunResult', stop by manual." << std::endl;
        testResult = actArg->setRunResult;
        stop_loop = 1;
        break;
	default: break;
    }
    return;
}

bool pending_run(void*arg)
{
    int count = 0;

    while(1)
    {
        std::cout << "demo_pending_run : " << count << std::endl;
        if(stop_loop!=0)
            break;

        count ++;
        if(count>2)
        {
            memcpy(status, "ok, as count is bigger than 2.", sizeof(status));
            std::cout << "ok, as count is bigger than 2." << std::endl;
        }else
        {
            memcpy(status, "not yet", sizeof(status));
            std::cout << "not yet." << std::endl;
        }
        sleep(1);
    }

    if(testResult == TestResultAsPass)
    {
        return TestResultAsPass;
    }
    return TestResultAsFail;
}

char * pending_getPendingData(void*arg)
{
    return status;
}
